package com.jumper.study.admin.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jumper.study.admin.dto.req.EducationSearchReq;
import com.jumper.study.admin.dto.resp.StudentLessonResp;
import com.jumper.study.admin.dto.resp.StudentStudyLogResp;
import com.jumper.study.admin.dto.req.LessonOpenBatchReq;
import com.jumper.study.admin.service.StudentLessonService;
import com.jumper.study.admin.service.StudentStudyLogService;
import com.jumper.study.common.api.CommonPageResult;
import com.jumper.study.common.api.CommonResult;
import com.jumper.study.db.dto.StudentLessonDto;
import com.jumper.study.db.dto.StudentStudyLogDto;
import com.jumper.study.db.model.StudentLesson;
import com.jumper.study.common.util.AdminUtil;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.validation.annotation.Validated;




@RestController
@RequestMapping("/api/backend/educations")
public class EducationController {
    
    @Autowired
    private StudentLessonService studentLessonService;

    @Autowired
    private StudentStudyLogService studyLogService;

    @Autowired
    private AdminUtil adminUtil;

    @PostMapping("")
    public CommonPageResult<List<StudentLessonResp>> getList(@RequestBody EducationSearchReq req) {
        Page<StudentLessonDto> page = Page.of(req.getPage(), req.getLimit());
        List<StudentLessonDto> dtos = studentLessonService.pageSearchWithAccountAndLesson(page, req).getRecords();
        List<StudentLessonResp> resp = dtos.stream().map(
            dto -> new StudentLessonResp(dto.getId(), dto.getAccountId(), dto.getStudentName(), dto.getLessonId(), dto.getLessonTitle(),
                dto.getExpireTime(), dto.getAdminId(), dto.getStudyTimes(), dto.getCreatedAt())
        ).toList();

        return CommonPageResult.restPage(page.getCurrent(), page.getSize(), page.getTotal(), resp);
    }
    
    @DeleteMapping("/{id}")
    public CommonResult<Boolean> close(@PathVariable Long id) {
        StudentLesson sLesson = studentLessonService.getById(id);
        if (sLesson == null) {
            return CommonResult.success(true);
        }
        sLesson.setAdminId(adminUtil.getAdminId());
        boolean result = studentLessonService.closeLesson(sLesson);

        return CommonResult.success(result);
    }

    @PostMapping("/open")
    public CommonResult<Boolean> openBatch(@RequestBody @Validated LessonOpenBatchReq req) {
        //异步批量开课
        studentLessonService.openLessonBatch(req, adminUtil.getAdminId());

        return CommonResult.success(true);
    }

    //读取学员课程学习情况
    @GetMapping("/student")
    public CommonPageResult<List<StudentLessonResp>> studentLessons(@RequestParam(required = true) Long accountId, 
        @RequestParam(name = "page", defaultValue = "1") Long p, @RequestParam(defaultValue = "15") Long limit) {
        Page<StudentLessonDto> page = Page.of(p, limit);
        List<StudentLessonDto> dtos = studentLessonService.pageSearchWithLessonByAccountId(page, accountId).getRecords();
        List<StudentLessonResp> resp = dtos.stream().map(
            dto -> new StudentLessonResp(dto.getId(), dto.getAccountId(), dto.getStudentName(), dto.getLessonId(), dto.getLessonTitle(),
                dto.getExpireTime(), dto.getAdminId(), dto.getStudyTimes(), dto.getCreatedAt())
        ).toList();

        return CommonPageResult.restPage(page.getCurrent(), page.getSize(), page.getTotal(), resp);
    }

    //学员课程学习详细日志
    @GetMapping("/student/logs")
    public CommonResult<Object> studentLessonLogs(@RequestParam(required = true) Long accountId, @RequestParam(required = true) Long lessonId,
        @RequestParam(name = "page", defaultValue = "1") Long p, @RequestParam(defaultValue = "15") Long limit) {
        Page<StudentStudyLogDto> page = Page.of(p, limit);
        List<StudentStudyLogDto> dtos = studyLogService.pageSearchWithAccountAndChapterByAccountAndLessonId(page, accountId, lessonId).getRecords();
        List<StudentStudyLogResp> resp = dtos.stream().map(
            dto -> new StudentStudyLogResp(dto.getId(), dto.getAccountId(), dto.getStudentName(), dto.getLessonId(), dto.getChapterId(),
                dto.getChapterTitle(), dto.getEnterTime(), dto.getLeaveTime())
        ).toList();

        return CommonPageResult.restPage(page.getCurrent(), page.getSize(), page.getTotal(), resp);
    }
    
    
    //读取课程下学员学习情况
    @GetMapping("/lesson")
    public CommonPageResult<List<StudentLessonResp>> lessonStudents(@RequestParam(required = true) Long lessonId, @RequestParam(name = "page", defaultValue = "1") Long p, 
        @RequestParam(defaultValue = "15") Long limit) {
        Page<StudentLessonDto> page = Page.of(p, limit);
        List<StudentLessonDto> dtos = studentLessonService.pageSearchWithAccountByLessonId(page, lessonId).getRecords();
        List<StudentLessonResp> resp = dtos.stream().map(
            dto -> new StudentLessonResp(dto.getId(), dto.getAccountId(), dto.getStudentName(), dto.getLessonId(), dto.getLessonTitle(),
                dto.getExpireTime(), dto.getAdminId(), dto.getStudyTimes(), dto.getCreatedAt())
        ).toList();

        return CommonPageResult.restPage(page.getCurrent(), page.getSize(), page.getTotal(), resp);
    }
    
    @GetMapping("/lesson/logs")
    public CommonResult<Object> lessonStudyLogs(@RequestParam(required = true) Long lessonId, @RequestParam(name = "page", defaultValue = "1") Long p,
        @RequestParam(defaultValue = "15") Long limit) {
        Page<StudentStudyLogDto> page = Page.of(p, limit);
        List<StudentStudyLogDto> dtos = studyLogService.pageSearchWithAccountAndChapterByLessonId(page, lessonId).getRecords();
        List<StudentStudyLogResp> resp = dtos.stream().map(
            dto -> new StudentStudyLogResp(dto.getId(), dto.getAccountId(), dto.getStudentName(), dto.getLessonId(), dto.getChapterId(), dto.getChapterTitle(),
                dto.getEnterTime(), dto.getLeaveTime())
        ).toList();

        return CommonPageResult.restPage(page.getCurrent(), page.getSize(), page.getTotal(), resp);
    }
    
}
